package com.quzitech.efest.adapter;

import java.util.ArrayList;
import java.util.List;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.quzitech.efest.R;
import com.quzitech.efest.activitys.base.BaseApplication;

public class DeviceAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<BluetoothDevice> mLeDevices = new ArrayList<BluetoothDevice>();
	int position_connect = -1;
	boolean position_status = false;
	private String mac = "";

	public void addDevice(BluetoothDevice device) {
		if (!mLeDevices.contains(device)) {
			mLeDevices.add(device);
		}
	}

	public void addDeviceFirst(BluetoothDevice device) {
		if (!mLeDevices.contains(device)) {
			mLeDevices.add(0, device);
		}
	}

	public void clear() {
		mLeDevices.clear();
		notifyDataSetChanged();
	}

	public DeviceAdapter(Context context) {
		this.context = context;

	}

	public void setMacSelect(String mac) {

		for (int i = 0; i < mLeDevices.size(); i++) {
			if (mLeDevices.get(i).getAddress().equals(mac)) {
				setStatus(i, true);
				notifyDataSetChanged();
				break;
			}
		}
	}

	// public void update(List<Competition> competitionlist) {
	// if (competitionlist == null) {
	// return;
	// }
	// this.list_competition = competitionlist;
	// updateCheck(ifChecked);
	// this.notifyDataSetChanged();
	//
	// }
	public void setStatus(int position, boolean ifConnected) {
		this.position_connect = position;
		this.position_status = ifConnected;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mLeDevices.size();
	}

	@Override
	public Object getItem(int position) {
		return mLeDevices.get(position);
	}

	@Override
	public long getItemId(int positioni) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.view_device_lvitem,
					null);
			holder = new ViewHolder(convertView);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_itemname
				.setText(mLeDevices.get(position).getName() != null ? mLeDevices
						.get(position).getName().trim()
						: "");

		if (position_connect == position) {
			if (position_status) {
				holder.tv_itemstatus.setText(R.string.device_connected);
				holder.iv_itemicon
						.setImageResource(R.drawable.device_connected);
			} else {
				holder.tv_itemstatus.setText(R.string.device_disconnect);
				holder.iv_itemicon
						.setImageResource(R.drawable.device_disconnect);
			}
		} else {
			holder.tv_itemstatus.setText(R.string.device_disconnect);
			holder.iv_itemicon.setImageResource(R.drawable.device_disconnect);
		}
		return convertView;

	}

	class ViewHolder {

		ImageView iv_itemicon;
		TextView tv_itemname, tv_itemstatus;

		public ViewHolder(View view) {
			iv_itemicon = (ImageView) view.findViewById(R.id.iv_itemicon);
			tv_itemname = (TextView) view.findViewById(R.id.tv_itemname);
			tv_itemstatus = (TextView) view.findViewById(R.id.tv_itemstatus);

			view.setTag(this);
		}
	}

}
